package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.JsonResult.JsonResult;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.*;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {
    //es
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private IDestinationEsService destinationEsService;
    @Autowired
    private ITravelEsService travelEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService;
    //mongo
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISearchService searchService;

    //使用switch通过传过来的type来选择使用哪个方法
    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        //url传入中文时，会自动将中文进行编码，此时需要解码
        String kw = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(kw);
        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
            case SearchQueryObject.TYPE_USERINFO:
                return searchUserInfo(qo);
            default:return searchAll(qo);
        }
    }
    //搜索目的地
    private Object searchDest(SearchQueryObject qo) {
        //返回值
        //result
        //dest
        //qo
        Destination destination=destinationService.queryName(qo.getKeyword());
        SearchResultVO vo=new SearchResultVO();
        if (destination !=null){
            List<Strategy> strategies=strategyService.queryByDestName(qo.getKeyword());
            vo.setStrategies(strategies);
            List<Travel> travels=travelService.queryByDestName(qo.getKeyword());
            vo.setTravels(travels);
            List<UserInfo> userInfos=userInfoService.queryCity(qo.getKeyword());
            vo.setUserInfos(userInfos);
            long total=strategies.size()+travels.size()+userInfos.size();
            vo.setTotal(total);
            Map<String,Object> map=new HashMap<>();
            map.put("result",vo);
            map.put("dest",destination);
            map.put("qo",qo);
            return JsonResult.success(map);
        }
        return JsonResult.success();
    }
    //搜索攻略
    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        //通过页面输入关键词进行全文搜索
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }
    //搜索游记
    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title", "summary");
        //通过页面输入关键词进行全文搜索
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }
    //搜索用户
    private Object searchUserInfo(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "city", "info");
        //通过页面输入关键词进行全文搜索
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }
    //搜索所有
    private Object searchAll(SearchQueryObject qo) {
        SearchResultVO vo=new SearchResultVO();
        Long total=0L;
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        vo.setStrategies(sts.getContent());
        total+=sts.getTotalElements();
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title", "summary");
        vo.setTravels(ts.getContent());
        total+=ts.getTotalElements();
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "info", "city");
        vo.setUserInfos(us.getContent());
        total+=us.getTotalElements();
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo, "name", "info");
        vo.setDestinations(ds.getContent());
        total+=ds.getTotalElements();
        vo.setTotal(total);
        //通过页面输入关键词进行全文搜索
        Map<String,Object> map=new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        return JsonResult.success(map);
    }




}
